using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace XnaDevRu.ContentBuilder.Framework
{
	/// <summary>
	/// Represents build logger which logs to specified TextBoxBase.
	/// </summary>
    public class TextBoxLogger : Microsoft.Build.Utilities.Logger
    {
        private TextBoxBase _textBox;
        private int warnings = 0;
        private int errors = 0;

		/// <summary>
		/// Initializes new TextBoxLogger instance.
		/// </summary>
		/// <param name="textBox">Text box to log to.</param>
        public TextBoxLogger(TextBoxBase textBox) : base()
        {
            if (textBox == null)
                throw new ArgumentNullException("textBox");

            _textBox = textBox;
        }

		/// <summary>
		/// Gets or sets text box to log to.
		/// </summary>
        public TextBoxBase TextBox { get { return _textBox; } set { _textBox = value; } }

		/// <summary>
		/// Initializes logger instance.
		/// </summary>
		/// <param name="eventSource">Event source for the logger.</param>
        public override void Initialize(Microsoft.Build.Framework.IEventSource eventSource)
        {
            //eventSource.AnyEventRaised += new Microsoft.Build.Framework.AnyEventHandler(eventSource_AnyEventRaised);
            eventSource.BuildFinished += new Microsoft.Build.Framework.BuildFinishedEventHandler(eventSource_BuildFinished);
            eventSource.BuildStarted += new Microsoft.Build.Framework.BuildStartedEventHandler(eventSource_BuildStarted);
            eventSource.CustomEventRaised += new Microsoft.Build.Framework.CustomBuildEventHandler(eventSource_CustomEventRaised);
            eventSource.ErrorRaised += new Microsoft.Build.Framework.BuildErrorEventHandler(eventSource_ErrorRaised);
            eventSource.MessageRaised += new Microsoft.Build.Framework.BuildMessageEventHandler(eventSource_MessageRaised);
            eventSource.ProjectFinished += new Microsoft.Build.Framework.ProjectFinishedEventHandler(eventSource_ProjectFinished);
            eventSource.ProjectStarted += new Microsoft.Build.Framework.ProjectStartedEventHandler(eventSource_ProjectStarted);
            //eventSource.StatusEventRaised += new Microsoft.Build.Framework.BuildStatusEventHandler(eventSource_StatusEventRaised);
            eventSource.TargetFinished += new Microsoft.Build.Framework.TargetFinishedEventHandler(eventSource_TargetFinished);
            eventSource.TargetStarted += new Microsoft.Build.Framework.TargetStartedEventHandler(eventSource_TargetStarted);
            eventSource.TaskFinished += new Microsoft.Build.Framework.TaskFinishedEventHandler(eventSource_TaskFinished);
            eventSource.TaskStarted += new Microsoft.Build.Framework.TaskStartedEventHandler(eventSource_TaskStarted);
            eventSource.WarningRaised += new Microsoft.Build.Framework.BuildWarningEventHandler(eventSource_WarningRaised);
        }

        private void eventSource_WarningRaised(object sender, Microsoft.Build.Framework.BuildWarningEventArgs e)
        {
            warnings++;
            _textBox.Text += Environment.NewLine + "Warning:" + e.Message + "\n";
        }

		private void eventSource_TaskStarted(object sender, Microsoft.Build.Framework.TaskStartedEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
        }

		private void eventSource_TaskFinished(object sender, Microsoft.Build.Framework.TaskFinishedEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
        }

		private void eventSource_TargetStarted(object sender, Microsoft.Build.Framework.TargetStartedEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
        }

		private void eventSource_TargetFinished(object sender, Microsoft.Build.Framework.TargetFinishedEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
        }

		private void eventSource_StatusEventRaised(object sender, Microsoft.Build.Framework.BuildStatusEventArgs e)
        {
            _textBox.Text += Environment.NewLine + "Status Event";
        }

		private void eventSource_ProjectStarted(object sender, Microsoft.Build.Framework.ProjectStartedEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
        }

		private void eventSource_ProjectFinished(object sender, Microsoft.Build.Framework.ProjectFinishedEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
        }

		private void eventSource_MessageRaised(object sender, Microsoft.Build.Framework.BuildMessageEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
        }

		private void eventSource_ErrorRaised(object sender, Microsoft.Build.Framework.BuildErrorEventArgs e)
        {
            errors++;
            _textBox.Text += Environment.NewLine + "Error:" + e.Message + "\n";
        }

		private void eventSource_CustomEventRaised(object sender, Microsoft.Build.Framework.CustomBuildEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
        }

		private void eventSource_BuildStarted(object sender, Microsoft.Build.Framework.BuildStartedEventArgs e)
        {
            errors = 0;
            warnings = 0;
            _textBox.Text = e.Message;
        }

		private void eventSource_BuildFinished(object sender, Microsoft.Build.Framework.BuildFinishedEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.Message + "\n";
            _textBox.Text += Environment.NewLine + errors.ToString() + " errors, " + warnings.ToString() + " warnings.";
        }

		private void eventSource_AnyEventRaised(object sender, Microsoft.Build.Framework.BuildEventArgs e)
        {
            _textBox.Text += Environment.NewLine + e.ToString();
        }
    }
}
